﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;
using BLL;
using WcfServices.ServiceInterfaces;
using EntitiesLayer;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using WcfServices.Utilities;
using WcfServices.ServiceInterfaces;
using System.IO;
using System.Drawing;

namespace WcfServices
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

    public class MaquinasService : IMaquinaService
    {

        private GestorMaquinasEjercicio gestor;
        public MaquinasService()
        {
            gestor = new GestorMaquinasEjercicio();
        }

        public string Listar()
        {
            IEnumerable<Maquina> listaMaquinas =
                Util.interceptWithReturn(gestor.listarMaquinas);

            Type[] ignoreTypes =  { typeof(IList<>), typeof(Bitmap) };
            string[] ignoreProperties = { "imagenBase64" };

            if (listaMaquinas != null)
                //return listaMaquinas.JsonSerialize(ignoreTypes: new[] { typeof(IList<>) });
                //return JsonConvert.SerializeObject(listaMaquinas, new StringEnumConverter());
                return listaMaquinas.JsonSerialize(ignoreTypes: ignoreTypes, ignorePropertie: ignoreProperties);

            return "";
        }

        public string BuscarPorId(int id)
        {
            Type[] ignoreTypes =  { typeof(IList<>), typeof(Bitmap) };
            string[] ignoreProperties = { "imagenBase64" };
            
            Maquina maquina =
               Util.interceptWithReturn(() => gestor.getInfoMaquinaById(id));

            if (maquina != null)
                //return JsonConvert.SerializeObject(maquina, new StringEnumConverter());
                return maquina.JsonSerialize(ignoreTypes: ignoreTypes, ignorePropertie: ignoreProperties);

            return "";

        }

        public void Create(Stream json)
        {
            IDictionary<string, string> data = json.DeserializeJson<Dictionary<string, string>>();
            Util.interceptWithNoReturn(
                () =>
                gestor.insertarMaquina(data["nombreMaquina"], data["numeroSerie"],
                Convert.ToInt32(data["numeroMaquina"]), data["indicacionesUso"], Convert.ToInt32(data["idTipoMaquina"]),
                Convert.ToChar(data["estado"]), data["fotoMaquina"].ToImageByte().toBitMap())
            );
        }

        public void Update(Stream json)
        {
         
            IDictionary<string, string> data = json.DeserializeJson<Dictionary<string, string>>();

            gestor.modificarMaquina(Convert.ToInt32(data["Id"]),
                data["NombreMaquina"], data["NumeroSerie"], Convert.ToInt32(data["NumeroMaquina"]),
                data["IndicacionesUso"], Convert.ToInt32(data["IdTipoMaquina"]), Convert.ToChar(data["Estado"]),
                data["ImagenBase64"].ToImageByte().toBitMap());

            /*
            dynamic data = json.DeserializeJson<dynamic>();
            gestor.modificarMaquina(
                Convert.ToInt32(data.idMaquina),
                data.nombreMaquina.ToString(), data.numeroSerie.ToString(), Convert.ToInt32(data.numeroMaquina),
                data.indicacionesUso.ToString(), Convert.ToInt32(data.idTipoMaquina), Convert.ToChar(data.estado.ToString()),
                data.fotoMaquina.ToImageByte().toBitMap());
            */
        }

        public void Delete(int id)
        {
            gestor.eliminarMaquina(id);
        }


    }
}
